{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/t81_558_deep_learning/blob/master/manual_setup.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T81-558: Applications of Deep Neural Networks\n",
    "**Manual Python Setup**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Software Installation\n",
    "\n",
    "This notebook described how to install PyTorch for GPU (cuda), Apple Metal (MLS), or CPU.\n",
    "\n",
    "## Installing Python and PyTorch\n",
    "\n",
    "It is possible to install and run Python/PyTorch entirely from your computer, without the need for Google CoLab. Running PyTorch locally does require some software configuration and installation.  If you are not confortable with software installation, just use Google CoLab.  These instructions show you how to install PyTorch for CPU, GPU (cuda), and Apple M1/M2/Mx Metal Performance Shaders (MPS). Many of the examples in this class will achieve considerable performance improvement from a GPU/MPS.\n",
    "\n",
    "The first step is to install Python 3.9.  I recommend using the Miniconda (Anaconda) release of Python, as it already includes many of the data science related packages that are needed by this class.  Anaconda directly supports Windows, Mac, and Linux. If you have a Mac and wish to use M1 MPS make sure to install the ARM64 version of Miniconda.  Miniconda is the minimal set of features from the extensive Anaconda Python distribution.  Download Miniconda from the following URL:\n",
    "\n",
    "* [Miniconda](https://docs.conda.io/en/latest/miniconda.html)\n",
    "\n",
    "Make sure that you select the Miniconda version that corrisponds to your operating system. It is particularly important to choose M1/Metal if you have a later (non-Intel) Mac.\n",
    "\n",
    "Once you've installed Miniconda, we will first install Jupyter, which is the editor you will use in this course.\n",
    "\n",
    "```\n",
    "conda install -y jupyter\n",
    "```\n",
    "\n",
    "You must make sure that PyTorch has the version of Python that it is compatible with.  The best way to accomplish this is with an Anaconda environment.  Each environment that you create can have its own Python version, drivers, and Python libraries.  I suggest that you create an environment to hold the Python instance for this class.  Use the following command to create your environment. I am calling the environment **torch**, you can name yours whatever you like. We will create this environment from a YML configuration file. You should select from one of the following commands:\n",
    "\n",
    "\n",
    "* **Mac M1/M2**: conda env create -f torch-conda.yml\n",
    "* **NVIDIA CUDA GPU**: conda env create -f torch-cuda.yml\n",
    "* **CPU Only**: conda env create -f torch.yml\n",
    "\n",
    "To enter this environment, you must use the following command: \n",
    "\n",
    "```\n",
    "conda activate torch\n",
    "```\n",
    "\n",
    "\n",
    "## Register your Environment\n",
    "\n",
    "The following command registers your **pytorch** environment. Again, make sure you \"conda activate\" your new **pytorch** environment.\n",
    "\n",
    "```\n",
    "python -m ipykernel install --user --name pytorch --display-name \"Python 3.9 (torch)\"\n",
    "```\n",
    "\n",
    "## Testing your Environment\n",
    "\n",
    "You can now start Jupyter notebook.  Use the following command.\n",
    "\n",
    "```\n",
    "jupyter notebook\n",
    "```\n",
    "\n",
    "You can now run the following code to check that you have the versions expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python Platform: macOS-12.4-arm64-arm-64bit\n",
      "PyTorch Version: 1.12.0.post2\n",
      "\n",
      "Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 17:00:33) \n",
      "[Clang 13.0.1 ]\n",
      "Pandas 1.4.3\n",
      "Scikit-Learn 1.1.1\n",
      "GPU is NOT AVAILABLE\n",
      "MPS (Apple Metal) is available\n",
      "Target device is mps\n"
     ]
    }
   ],
   "source": [
    "# What version of Python do you have?\n",
    "import sys\n",
    "import platform\n",
    "import torch\n",
    "import pandas as pd\n",
    "import sklearn as sk\n",
    "\n",
    "has_gpu = torch.cuda.is_available()\n",
    "has_mps = getattr(torch,'has_mps',False)\n",
    "device = \"mps\" if getattr(torch,'has_mps',False) \\\n",
    "    else \"gpu\" if torch.cuda.is_available() else \"cpu\"\n",
    "\n",
    "print(f\"Python Platform: {platform.platform()}\")\n",
    "print(f\"PyTorch Version: {torch.__version__}\")\n",
    "print()\n",
    "print(f\"Python {sys.version}\")\n",
    "print(f\"Pandas {pd.__version__}\")\n",
    "print(f\"Scikit-Learn {sk.__version__}\")\n",
    "print(\"GPU is\", \"available\" if has_gpu else \"NOT AVAILABLE\")\n",
    "print(\"MPS (Apple Metal) is\", \"AVAILABLE\" if has_mps else \"NOT AVAILABLE\")\n",
    "print(f\"Target device is {device}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
